#include "System_reset.h"
#include "stm32f10x.h"
#include "UART3.h"

void ResetKey_Init(void) {
    GPIO_InitTypeDef GPIO_InitStructure;
    EXTI_InitTypeDef EXTI_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
    
    // 配置按键引脚为上拉输入
    GPIO_InitStructure.GPIO_Pin = RESET_KEY_PIN;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // 上拉输入，按下时为低电平
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(RESET_KEY_PORT, &GPIO_InitStructure);
    
    // 配置外部中断映射
    GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
    
    // 配置EXTI
    EXTI_InitStructure.EXTI_Line = RESET_KEY_EXTI_LINE;
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; // 双边沿触发
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    
    // 配置NVIC
    NVIC_InitStructure.NVIC_IRQChannel = RESET_KEY_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 6; // 低于FreeRTOS内核优先级
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}
#include "UART3.h"
#include "flash.h"


extern volatile uint8_t reset_key_pressed ;
extern volatile uint32_t reset_press_time;
void EXTI0_IRQHandler(void) {
    if (EXTI_GetITStatus(RESET_KEY_EXTI_LINE) != RESET) {
        // 清除中断标志
		printf3("nihao\r\n");
        EXTI_ClearITPendingBit(RESET_KEY_EXTI_LINE);
        
        // 读取按键状态（假设低电平有效）
        if (GPIO_ReadInputDataBit(RESET_KEY_PORT, RESET_KEY_PIN) == 0) {
            reset_key_pressed = 1;
            reset_press_time = 0; 
			// 重置计时
        } else {
            reset_key_pressed = 0;
        }
    }
}
